package com.haierp.controller.authority;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.utils.DigestUtils;
import com.haierp.model.authority.UserHai;
import com.haierp.service.authority.IUserServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.authority.UserQueryVO;

/**
 * 
 * Title: UserController.java
 * Description: 
 *
 * @author jc
 * Apr 18, 2017
 *
 */
@Service("userControllerHai")
@Controller
@RequestMapping("/userHai")
public class UserControllerHai {

	@Autowired
	private IUserServiceHai iUserServiceHai;

	@RequestMapping("/add")
	@ResponseBody
	public Object add(UserQueryVO userQueryVO) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null == userQueryVO.getId()) {
			List<UserHai> list = iUserServiceHai.queryByLoginName(userQueryVO);
	        if (list != null && !list.isEmpty()) {
	            result.buildMsg("用户名已存在!").buildIsSuccess(false);
	        } else if(StringUtils.isBlank(userQueryVO.getPassword())) {
	        	result.buildMsg("用户密码不能为空!").buildIsSuccess(false);
	        } else {
		        userQueryVO.setPassword(DigestUtils.md5Hex(userQueryVO.getPassword()));
		        userQueryVO.setCreateTime(new Date());
		        iUserServiceHai.insertByVo(userQueryVO);
		        result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(UserQueryVO userQueryVO) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != userQueryVO.getId()) {
			if(StringUtils.isBlank(userQueryVO.getPassword())) {
	        	return result.buildMsg("用户密码不能为空!").buildIsSuccess(false);
	        }
			
			List<UserHai> list = iUserServiceHai.queryByLoginName(userQueryVO);

	        if ((list != null) && (list.size() > 0)) {
	        	
	        	if (list.size() == 1) {
	        		if (list.get(0).getId() != userQueryVO.getId()) {
	        			return result.buildMsg("登录名已经存在!").buildIsSuccess(false);
					}
				} else if (list.size() > 1) {
					return result.buildMsg("登录名已经存在!").buildIsSuccess(false);
				}
	        } 
	        
        	userQueryVO.setPassword(DigestUtils.md5Hex(userQueryVO.getPassword()));
        	
	        iUserServiceHai.updateByVo(userQueryVO);
	        return result.buildIsSuccess(true);
		} 
		
		return result.buildMsg("错误数据").buildIsSuccess(false);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();

		if (null != id) {
			iUserServiceHai.deleteUserById(id);
			result.buildIsSuccess(true);
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<UserQueryVO> result = new JsonResult<>();
		
		return result.buildData(iUserServiceHai.queryVoById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/queryList")
	@ResponseBody
	public Object queryList(UserQueryVO userQueryVO) {
		JsonPageResult<List<UserQueryVO>> result = iUserServiceHai.queryUserQueryVOList(userQueryVO);
		return result.buildIsSuccess(true);
	}
}
